#!/usr/local/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) The PyAMF Project.
# See LICENSE.txt for details.

import kernel.base.data as user_data
import kernel.base.function as func

class chat():
    names=False
    def __init__(self):
        self.names="chat"
        
    #世界聊天
    def world(self,user_id,data):
        print("欢迎进入世界聊天")
        #过滤非法字符串
        data=func.keyfilter(user_data,data)
        #禁言处理
        if(self.gagc(user_id,"world")==False):
            return False;
        
        #聊天速度处理
        if(self.chat_times(user_id)==False):
            return False;
        
        get_user_list=user_data.get_user_list()
        sendss=self.sendinfo(user_id,data,"world")
        
        if(get_user_list):
            for item in get_user_list:
                if(get_user_list[item]["user_id"]!=user_id):
                    fp=get_user_list[item]["fp"]
                    if(fp):
                        sendss["get_nickname"]=get_user_list[item]["nickname"]
                        print(func.json.dumps(sendss))
                        func.sendsocet(fp,sendss)
        return False
    
    #双方私聊
    def whisper(self,user_id,nickname,data):
        #过滤非法字符串
        data=func.keyfilter(user_data,data)
        #禁言处理
        if(self.gagc(user_id,"whisper")==False):
            return False;
        
        #聊天速度处理
        if(self.chat_times(user_id)==False):
            return False;
        
        getuser=user_data.nntoinfo(nickname)
        sendss=self.sendinfo(user_id,data,"whisper")
        
        if(getuser):
            fp=getuser["fp"]
            if(fp):
                func.sendsocet(fp,sendss)
        return False
        
    #联盟聊天
    def alliance(self,user_id,alliance_id,data):
        #过滤非法字符串
        data=func.keyfilter(user_data,data)
        #禁言处理
        if(self.gagc(user_id,"alliance")==False):
            return False;
        
        #聊天速度处理
        if(self.chat_times(user_id)==False):
            return False;
        
        if(alliance_id>0):
            getuser_list=user_data.unionlist(alliance_id)
            sendss=self.sendinfo(user_id,data,"alliance")
            
            if(getuser_list):
                for item in getuser_list:
                    getuid=getuser_list[item]
                    if(getuid):
                        fp=user_data.get_user(getuid,"fp")
                        if(fp):
                            func.sendsocet(fp,sendss)
        return False
    
    def show(self,user_id,data):
        """
        @info 附近聊天
        @param int data 聊天内容
        @return array 物品信息
        """
        map_id=user_data.get_user(user_id, "map_id")
        print("欢迎进入附近聊天")
        #过滤非法字符串
        data=func.keyfilter(user_data,data)
        #禁言处理
        if(self.gagc(user_id,"show")==False):
            return False;
        
        #聊天速度处理
        if(self.chat_times(user_id)==False):
            return False;
        
        get_user_list=user_data.get_user_list()
        sendss=self.sendinfo(user_id,data,"show")
        
        if(get_user_list):
            for item in get_user_list:
                if(get_user_list[item]["user_id"]!=user_id and get_user_list[item]["map_id"]==map_id):
                    fp=get_user_list[item]["fp"]
                    if(fp):
                        sendss["get_nickname"]=get_user_list[item]["nickname"]
                        print(func.json.dumps(sendss))
                        func.sendsocet(fp,sendss)
        return False
    
    def team(self,user_id,data):
        """
        @info 组队聊天
        @param int data 聊天内容
        @return array 物品信息
        """
        team=user_data.get_user(user_id, "team")
        print("欢迎进入附近聊天")
        #过滤非法字符串
        data=func.keyfilter(user_data,data)
        #禁言处理
        if(self.gagc(user_id,"show")==False):
            return False;
        
        #聊天速度处理
        if(self.chat_times(user_id)==False):
            return False;
        
        get_user_list=user_data.get_user_list()
        sendss=self.sendinfo(user_id,data,"show")
        
        if(get_user_list):
            for item in get_user_list:
                if(get_user_list[item]["user_id"]!=user_id and get_user_list[item]["team"]==team):
                    fp=get_user_list[item]["fp"]
                    if(fp):
                        sendss["get_nickname"]=get_user_list[item]["nickname"]
                        print(func.json.dumps(sendss))
                        func.sendsocet(fp,sendss)
        return False
    
    #构建发送信息
    def sendinfo(self,user_id,data,module):
        sendnickname=user_data.get_user(user_id,"nickname")
        returnsss={"res":True,"data":data,"send_nickname":sendnickname}
        sendss=user_data.agreement("api.chat",module,returnsss)
        return sendss
    
    
    #禁言处理
    def gagc(self,user_id,module):
        gag=func.gag(user_data.storage(),user_id)
        if(gag>0):
            returnsss={"msg":1531,"replace":{"value":gag}}
            sendss=user_data.sysagreement(returnsss,"chat")
            fp=user_data.get_user(user_id,"fp")
            print(func.json.dumps(sendss)+"\n")
            func.sendsocet(fp,sendss)
            return False
        else:
            return True
        
    #聊天速度处理
    def chat_times(self,user_id):
        chat_time=user_data.get_user(user_id, "chat_time")
        if(func.time.time()-chat_time>2):
            user_data.user_property(user_id,"chat_time",func.time.time())
            return True
        else:
            returnsss={"msg":1533}
            sendss=user_data.sysagreement(returnsss,"chat")
            fp=user_data.get_user(user_id,"fp")
            print(func.json.dumps(sendss)+"\n")
            func.sendsocet(fp,sendss)
            return False
        